package com.itheima.mobile.controller;

import com.itheima.common.entity.Result;
import com.itheima.common.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;

@RestController
@RequestMapping("/validatecode")
@Slf4j
public class ValidateCodeController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("/send4Order")
    public Result send4Order(@RequestParam("telephone") String telephone) {
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        log.info("提交预约验证码是:{}", validateCode);
        redisTemplate.opsForValue().set(telephone, validateCode.toString());
        redisTemplate.expire(telephone, Duration.ofMinutes(5L));
        return Result.of(true, "发送验证码成功!", null);
    }

    @GetMapping("/send4Login")
    public Result send4Login(@RequestParam("telephone") String telephone){
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        log.info("登录验证码是:{}", validateCode);
        redisTemplate.opsForValue().set(telephone, validateCode.toString());
        redisTemplate.expire(telephone, Duration.ofMinutes(5L));
        return Result.of(true, "发送验证码成功!", null);
    }
}
